<?php
/**
 * 
 * App for managing contacts
 * 
 * @category Abovo
 * 
 * @package Abovo_App_Crm
 * 
 * @subpackage Abovo_App_Crm_Contacts
 * 
 * @license http://opensource.org/licenses/gpl-license.php GPL
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 */

/**
 * 
 * App to manage companies
 * 
 * This is part of the CRM module
 * 
 * @category Abovo
 * 
 * @package Abovo_App_Crm
 * 
 * @subpackage Abovo_App_Crm_Contacts
 * 
 */
class Abovo_App_Crm_Contacts extends Abovo_App_Crm {
    
    /**
     * 
     * Default action
     * 
     * @var string
     * 
     */
    protected $_action_default = 'search';
    
    /**
     * 
     * Contacts model
     * 
     * @var Abovo_Model_Contacts
     * 
     */
    protected $_contacts;
    
    /**
     * 
     * Contact info
     * 
     * @var Solar_Sql_Row
     * 
     */
    public $contact;
    
    /**
     * 
     * undocumented class variable
     * 
     * @var Solar_Sql_Rowset
     * 
     */
    public $contacts;
    
    /**
     * 
     * Contact form
     * 
     * @var Solar_Form
     * 
     */
    public $contact_form;
    
    /**
     * 
     * Browse contacts
     * 
     * @return void
     * 
     */
    public function actionSearch()
    {
        $select = Solar::factory('Solar_Sql_Select');
        
        $cols = array(
            'id',
            'firstname',
            'lastname',
            'email',
            'city',
        );
        
        $select->from('contacts', $cols)
               ->leftJoin('companies', 'contacts.companies_id=companies.id', 'name as company');
        
        $this->contacts = $select->fetch('rowset');
        
        // check for 'deleted' flash
        $this->_msgSuccess($this->_session->getFlash('deleted'));
    }
    
    /**
     * 
     * Add a contact
     * 
     * @return void
     * 
     */
    public function actionAdd()
    {
        $form = $this->_contacts->form();
        
        if ($this->_isProcess('save') && $form->validate()) {
            $values = $form->values();
            $info   = $this->_contacts->save($values['contact']);
            
            // Done. Forward to contact show-page.
            return $this->_forward('show', array($info['id']));
        }
        
        // assign form for view
        $this->contact_form = $form;
    }
    
    /**
     * 
     * Edit contact
     * 
     * Performs edit and delete
     * 
     * @return void
     * 
     */
    public function actionEdit($id = null)
    {
        $info = $this->_contacts->fetch((int) $id);
        
        if (count($info) === 0) {
            $this->errors[] = $this->locale('ERR_CONTACT_NOT_FOUND');
            $this->_action  = 'error';
            $this->_view    = 'error';
            
            // make sure we don't run anything after this
            return;
        }
        
        // assign for view
        $this->contact = $info;
        
        // set details about the page
        $this->_addBreadCrumb(
            $this->contact->lastname . ', ' . $this->contact->firstname,
            "/crm/contacts/show/$id"
        );
        
        // ------------------------------------
        //
        // Delete?
        //
        if($this->_isProcess('delete')) {
            $this->_contacts->delete(array('id = ?' => $id));
            $this->_session->setFlash('deleted', $this->locale('TEXT_CONTACT_DELETED'));
            $this->_redirect('crm/contacts/search');
            return;
        }
        
        
        // ------------------------------------
        //
        // Edit
        //
        
        $form = $this->_contacts->form($info);
        
        // add current company to options
        $form->elements['contact[companies_id]']['options'][$info->companies_id] = 
            Solar::factory('Abovo_Model_Company')->fetch($info->companies_id)->name; 
        
        if ($this->_isProcess('save')) {
            if ($form->validate()) {
                $values = $form->values();
                $values['contact']['id'] = $id;
                $this->_contacts->save($values['contact']);
                
                // add flash
                $this->_session->setFlash('saved', $form->feedback[0]);
                
                Solar::registry('log')->save(
                    get_class($this),
                    'notice',
                    'Saved info on contact '
                    . $values['contact']['firstname'] . ' '
                    . $values['contact']['lastname']
                );
                
                // Done. Forward to contact show-page.
                return $this->_redirect('crm/contacts/show/' . $id);
            }
        } else {
            // no submit attempt, populate with 'old' data
            $form->populate(array('contact' => $info->toArray()));
        }
        
        // assign form for view
        $this->contact_form = $form;
    }
    
    /**
     * 
     * Show info on one contact
     * 
     * @return void
     * 
     */
    public function actionShow($id = null)
    {
        $info = $this->_contacts->fetch((int) $id);
        
        if (count($info) === 0) {
            // contact doesn't exist
            $this->errors[] = $this->locale('ERR_CONTACT_NOT_FOUND');
            $this->_action = 'error';
            $this->_view = 'error';
            
            // make sure we don't run anything after this
            return;
        }
        
        // assign for view
        $this->contact = $info;
        
        // gender?
        if ($this->contact->gender == Abovo_Model_Contacts::GENDER_MALE) {
            $this->contact->gender = Solar::$locale->fetch(
                'Abovo_Model_Contacts',
                'TEXT_GENDER_MALE'
            );
        } else {
            $this->contact->gender = Solar::$locale->fetch(
                'Abovo_Model_Contacts',
                'TEXT_GENDER_FEMALE'
            );
        }
        
        // set details about the page
        $this->_addBreadCrumb($this->contact->lastname . ', ' . $this->contact->firstname);
        
        // add flash
        $this->_msgSuccess($this->_session->getFlash('saved'));
    }
    
    /**
     * 
     * Extended setup
     * 
     * @return void
     * 
     */
    public function _setup()
    {
        parent::_setup();
        $this->_contacts = Solar::factory('Abovo_Model_Contacts');
        
        $this->layout_local['crm/contacts/search'] = $this->locale(
            'APP_ACTION_SEARCH'
        );
        
        $this->layout_local['crm/contacts/add']    = $this->locale('APP_ACTION_ADD');
    }
}
